$(function() {
            const TABID = `tpl-table`;
            const SELECTID = `tpl-cate`;
            const PAGEBOXID = `pageBox`;
            const NULLERROR = `无对应信息`;
            const CONFIRM = `确认要删除吗?`;
            let articleQueryData = { //文章查询数据
                pagenum: 1, //页码值
                pagesize: 4, //页容量
                cate_id: ``, //分类Id
                state: ``, //发布状态
            }
            const pageInfo = {
                elem: PAGEBOXID,
                limit: articleQueryData.pagesize,
                prev: `prev`,
                next: `next`,
                first: `First`,
                last: `Last`,
                theme: `#009688`,
                jump: jumpPage
            }
            initArticleSelect();
            initArticleList();

            template.defaults.imports.dateToFormat = function(date) {
                const newDate = date.split(`.`);
                newDate.pop();
                return newDate.join(``);
            }
            $(`tbody`).on(`click`, `.btn-delete`, function() {
                        //删除对应的文章数据
                        layer.confirm(CONFIRM, () => {
                                    const $this = $(this);
                                    Ajax(`get`, `/my/article/delete/${$this.data(`id`)}`,{},function(res){
                                         layer.msg(res.message,{time:1000},function(){
                                             res.status?0:initArticleList();
                                         });    
                                    });
                        });                       
             }).on(`click`,`.btn-edit`,function(){
                 //编辑对应的文章数据
                 layer.open({
                     type:2,
                     title:`编辑文章信息`,
                     btn:`确认修改`,
                     area:[`90%`,`90%`],
                     content:`/article/art_pub.html?${$(this).data(`id`)}`,
                     yes:closeThis.bind(null),
                     end:()=>{
                          initArticleList();
                     }
                 });
             });
             function closeThis(index) {
                 layer.close(index);
             }
             window.closeThis=closeThis;
             $(`#form-search`).on(`submit`,function(e){
                 e.preventDefault();
                 let searchInfo = layui.form.val(`search`);
                 articleQueryData.cate_id = searchInfo.cate_id;
                 articleQueryData.state = searchInfo.state;
                 initArticleList(articleQueryData);
             })

    function initArticleList(data=articleQueryData,jump) {
        Ajax(`get`, `/my/article/list`, data, function(res) {
            if (res.status) return layer.msg(res.message);
            if (!res.data.length) return layer.msg(NULLERROR);
            if (!jump) layer.msg(res.message,{time:1000});
            pageInfo.count = res.total;
            const tableHtml = template(TABID, res.data);
            $(`tbody`).html(tableHtml);
            renderPage(pageInfo);
        })
    }

    function initArticleSelect() {
        Ajax(`get`, `/my/article/cates`, {}, function(res) {
            if (res.status) return layer.msg(res.message);
            const selectHtml = template(SELECTID, res.data);
            $(`select[name="cate_id"]`).html(selectHtml);
            layui.form.render(`select`);
        })
    }
    function renderPage(pageInfo) {
        layui.laypage.render(pageInfo);
    }
    function jumpPage(curPage,first){
        if (!first){
            articleQueryData.pagenum = curPage.curr;
            initArticleList(articleQueryData,true);
        }        
    }
})